package com.captain.special.链表;

import com.captain.leetcode.链表.ListNode;

import java.util.List;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/3/30 12:50
 */
public class 链表的中间结点876 {
    public ListNode middleNode(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode cur = new ListNode(-1, head);
        ListNode lenNode = head;
        int len = 0;
        while (lenNode != null) {
            len++;
            lenNode = lenNode.next;
        }
        int mid = 0;
        if (len % 2 != 0) {
            mid = len / 2 + 1;
        } else {
            mid = len / 2;
        }
        for (int i = 0; i < mid; i++) {
            cur = cur.next;
        }
        return cur;
    }
}
